﻿#include "freqa11item.h"

#include <QDebug>
#include <QGraphicsScene>

FreqA11Item::FreqA11Item(const QStringList& machineList, const QList<QStringList>& valueList, QObject *parent, QGraphicsItem *item) : QObject(parent), QGraphicsItem(item),
    m_machineList(machineList),
    m_valueList(valueList)
{
    //缓存在设备坐标系中，并且只有在位置、大小或外观发生变化时才需要重新计算。
    this->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
}

QRectF FreqA11Item::boundingRect() const
{
    return QRectF(0, 0, scene()->width(), 440);
}

void FreqA11Item::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);

    painter->setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing);
    painter->setCompositionMode(QPainter::CompositionMode_Source);

    QPen pen;
    pen.setColor(Qt::black);
    painter->setPen(pen);
    painter->setFont(QFont("Helvetica", 12));

    // 绘制网格线
    painter->drawLine(QLineF(30, 0, 870, 0));
    painter->drawLine(QLineF(30, 40, 870, 40));
    painter->drawLine(QLineF(30, 80, 870, 80));
    painter->drawLine(QLineF(30, 440, 870, 440));
    for (int i = 0; i < 9; i++)
        painter->drawLine(QLineF(130, 120 + i * 40, 870, 120 + i * 40));

    painter->drawLine(QLineF(30, 0, 30, 440));
    painter->drawLine(QLineF(80, 80, 80, 440));
    painter->drawLine(QLineF(130, 80, 130, 440));
    for (int i = 0; i < 5; i++)
        painter->drawLine(QLineF(260 + i * 120, 0, 260 + i * 120, 440));
    painter->drawLine(QLineF(870, 0, 870, 440));

    // 文字说明
    painter->drawText(QPointF(100, 25), QStringLiteral("项目或参数"));
    painter->drawText(QPointF(290, 25), QStringLiteral("I作动器"));
    painter->drawText(QPointF(410, 25), QStringLiteral("II作动器"));
    painter->drawText(QPointF(530, 25), QStringLiteral("III作动器"));
    painter->drawText(QPointF(650, 25), QStringLiteral("IV作动器"));
    painter->drawText(QPointF(770, 25), QStringLiteral("技术要求"));
    painter->drawText(QPointF(100, 65), QStringLiteral("角频率"));
    painter->drawText(QPointF(290, 65), QStringLiteral("幅值(dB)"));
    painter->drawText(QPointF(410, 65), QStringLiteral("幅值(dB)"));
    painter->drawText(QPointF(530, 65), QStringLiteral("幅值(dB)"));
    painter->drawText(QPointF(650, 65), QStringLiteral("幅值(dB)"));
    painter->drawText(QPointF(770, 65), QStringLiteral("幅值(dB)"));
    painter->drawText(QPointF(45, 160), QStringLiteral("频"));
    painter->drawText(QPointF(45, 185), QStringLiteral("率"));
    painter->drawText(QPointF(45, 210), QStringLiteral("特"));
    painter->drawText(QPointF(45, 235), QStringLiteral("性"));
    painter->drawText(QPointF(45, 260), QStringLiteral("1°"));
    painter->drawText(QPointF(45, 285), QStringLiteral("电"));
    painter->drawText(QPointF(45, 310), QStringLiteral("动"));
    painter->drawText(QPointF(45, 335), QStringLiteral("泵"));
    painter->drawText(QPointF(95, 210), QStringLiteral("位"));
    painter->drawText(QPointF(95, 235), QStringLiteral("移"));
    painter->drawText(QPointF(95, 260), QStringLiteral("表"));
    painter->drawText(QPointF(95, 285), QStringLiteral("决"));
    painter->drawText(QPointF(180, 105), QStringLiteral("2"));
    painter->drawText(QPointF(180, 145), QStringLiteral("15"));
    painter->drawText(QPointF(180, 185), QStringLiteral("30"));
    painter->drawText(QPointF(180, 225), QStringLiteral("60"));
    painter->drawText(QPointF(180, 265), QStringLiteral("70"));
    painter->drawText(QPointF(180, 305), QStringLiteral("80"));
    painter->drawText(QPointF(180, 345), QStringLiteral("100"));
    painter->drawText(QPointF(180, 385), QStringLiteral("130"));
    painter->drawText(QPointF(180, 425), QStringLiteral("150"));
    painter->drawText(QPointF(750, 105), QStringLiteral("≤0.5"));
    painter->drawText(QPointF(750, 145), QStringLiteral("≤0.5"));
    painter->drawText(QPointF(750, 185), QStringLiteral("≤0.8"));
    painter->drawText(QPointF(750, 225), QStringLiteral("≤1.0"));
    painter->drawText(QPointF(750, 265), QStringLiteral("≤0.2"));
    painter->drawText(QPointF(750, 305), QStringLiteral("≤-2.8"));
    painter->drawText(QPointF(750, 345), QStringLiteral("≤-5.5"));
    painter->drawText(QPointF(750, 385), QStringLiteral("≤-9.0"));
    painter->drawText(QPointF(750, 425), QStringLiteral("≤-14"));

    // 文字数值: 1, 2, 3, 4
    if (m_machineList.contains("A1")) {
        painter->drawText(QPointF(290, 105), m_valueList.at(0).at(0));
        painter->drawText(QPointF(290, 145), m_valueList.at(1).at(0));
        painter->drawText(QPointF(290, 185), m_valueList.at(2).at(0));
        painter->drawText(QPointF(290, 225), m_valueList.at(3).at(0));
        painter->drawText(QPointF(290, 265), m_valueList.at(4).at(0));
        painter->drawText(QPointF(290, 305), m_valueList.at(5).at(0));
        painter->drawText(QPointF(290, 345), m_valueList.at(6).at(0));
        painter->drawText(QPointF(290, 385), m_valueList.at(7).at(0));
        painter->drawText(QPointF(290, 425), m_valueList.at(8).at(0));
    }

    if (m_machineList.contains("A2")) {
        painter->drawText(QPointF(410, 105), m_valueList.at(0).at(1));
        painter->drawText(QPointF(410, 145), m_valueList.at(1).at(1));
        painter->drawText(QPointF(410, 185), m_valueList.at(2).at(1));
        painter->drawText(QPointF(410, 225), m_valueList.at(3).at(1));
        painter->drawText(QPointF(410, 265), m_valueList.at(4).at(1));
        painter->drawText(QPointF(410, 305), m_valueList.at(5).at(1));
        painter->drawText(QPointF(410, 345), m_valueList.at(6).at(1));
        painter->drawText(QPointF(410, 385), m_valueList.at(7).at(1));
        painter->drawText(QPointF(410, 425), m_valueList.at(8).at(1));
    }

    if (m_machineList.contains("A3")) {
        painter->drawText(QPointF(530, 105), m_valueList.at(0).at(2));
        painter->drawText(QPointF(530, 145), m_valueList.at(1).at(2));
        painter->drawText(QPointF(530, 185), m_valueList.at(2).at(2));
        painter->drawText(QPointF(530, 225), m_valueList.at(3).at(2));
        painter->drawText(QPointF(530, 265), m_valueList.at(4).at(2));
        painter->drawText(QPointF(530, 305), m_valueList.at(5).at(2));
        painter->drawText(QPointF(530, 345), m_valueList.at(6).at(2));
        painter->drawText(QPointF(530, 385), m_valueList.at(7).at(2));
        painter->drawText(QPointF(530, 425), m_valueList.at(8).at(2));
    }

    if (m_machineList.contains("A4")) {
        painter->drawText(QPointF(650, 105), m_valueList.at(0).at(3));
        painter->drawText(QPointF(650, 145), m_valueList.at(1).at(3));
        painter->drawText(QPointF(650, 185), m_valueList.at(2).at(3));
        painter->drawText(QPointF(650, 225), m_valueList.at(3).at(3));
        painter->drawText(QPointF(650, 265), m_valueList.at(4).at(3));
        painter->drawText(QPointF(650, 305), m_valueList.at(5).at(3));
        painter->drawText(QPointF(650, 345), m_valueList.at(6).at(3));
        painter->drawText(QPointF(650, 385), m_valueList.at(7).at(3));
        painter->drawText(QPointF(650, 425), m_valueList.at(8).at(3));
    }
}
